﻿//using LiteDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace MSH.LogClient
{
    /// <summary>
    /// LiteDB数据的存储、删除、读取
    /// </summary>
    internal static class LiteDBHelper
    {
        private const string DBName = "MSHLogData.db";

        public static void Save<T>(T obj, int logLevel, string configName)
            where T : class
        {
            try
            {
                //using (var trans = new TransactionScope())
                //{
                //    using (var db = new LiteDatabase(DBName))
                //    {
                //        var collection = db.GetCollection<LiteDbData<T>>(typeof(T).Name);
                //        collection.Insert(new LiteDbData<T>(obj, logLevel, configName));
                //    }
                //    trans.Complete();
                //}
            }
            catch
            {
            }
        }

        public static void Remove<T>(long id)
        {
            try
            {
                //using (var trans = new TransactionScope())
                //{
                //    using (var db = new LiteDatabase(DBName))
                //    {
                //        var collection = db.GetCollection<LiteDbData<T>>(typeof(T).Name);
                //        collection.Delete(a => a.Id == id);
                //    }
                //    trans.Complete();
                //}
            }
            catch
            {
            }
        }

        public static LiteDbData<T> Get<T>(string configName)
            where T : class
        {
            try
            {
                //using (var db = new LiteDatabase(DBName))
                //{
                //    var collection = db.GetCollection<LiteDbData<T>>(typeof(T).Name);
                //    var minId = collection.Find(a => a.ConfigName == configName).Min(a => a.Id);
                //    return collection.FindOne(a => a.Id == minId);
                //}
                return null;
            }
            catch
            {
                return null;
            }
        }
    }
}
